package pl.koziolekweb.szkola.CHInterfejsyKomp1;

// CH-InterfejsyKomp1/KompOs.java
 
import java.util.Comparator;

public class KompOs implements Comparator<Osoba> {

    public static enum Komp { PO_NAME,    PO_ROKU,
                              PO_NAMERev, PO_ROKURev };
    private Komp komp;

    public KompOs(Komp komp) {
        this.komp = komp;
    }

    public int compare(Osoba os1, Osoba os2) {

        int rRok = os1.getRok() - os2.getRok();
        int rNam = os1.getName().compareTo(os2.getName());

        int result = 0;

        switch (komp) {
            case PO_NAME:
                result = rNam != 0 ?  rNam : rRok;
                break;
            case PO_NAMERev:
                result = rNam != 0 ? -rNam : rRok;
                break;
            case PO_ROKU:
                result = rRok != 0 ?  rRok : rNam;
                break;
            case PO_ROKURev:
                result = rRok != 0 ? -rRok : rNam;
                break;
        }
        return result;
    }
}
